package com.oig.sys.auth.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbPermissionDao;
import com.oig.sys.auth.dto.PermissionDto;
import com.oig.sys.auth.entity.TbPermission;
import com.oig.sys.auth.service.ITbPermissionService;
import com.oig.sys.auth.vo.PermissionVo;
import com.oig.sys.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TbPermissionServiceImpl implements ITbPermissionService {

    private final ITbPermissionDao permissionDao;

    @Autowired
    public TbPermissionServiceImpl(ITbPermissionDao permissionDao){
        this.permissionDao = permissionDao;
    }


    @Override
    public TbPermission findNullAuthByUrl(String apiUrl) {
        return permissionDao.findByApiUrlAndFlag(apiUrl, CommonEnum.FlagEnum.TRUE_FLAG.getCode());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PermissionVo addPermission(PermissionDto permissionDto){
        log.debug("permissionDto:{}",permissionDto);
        TbPermission permission = new TbPermission();
        BeanUtils.copyProperties(permissionDto, permission, "id");
        permission.setCreateUserId(SecurityUtil.getDefineUserMain().getId());
        permission.setCreateUserName(SecurityUtil.getDefineUserMain().getRealName());
        permission.setCreateTime(new Date());
        permission.setLastUpdateId(permission.getCreateUserId());
        permission.setLastUpdateName(permission.getCreateUserName());
        permission.setLastUpdateTime(permission.getCreateTime());
        permission.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
        if (permission.getFlag()==null){
            permission.setFlag(CommonEnum.FlagEnum.FALSE_FLAG.getCode());
        }
        this.permissionDao.save(permission);
        PermissionVo permissionVo = new PermissionVo();
        BeanUtils.copyProperties(permission, permissionVo);
        return permissionVo;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public PermissionVo updatePermission(PermissionDto permissionDto) {
        TbPermission permission = this.permissionDao.getTbPermissionById(permissionDto.getId());
        log.debug("dto={},entity={}",permissionDto,permission);
        if (permission==null){
            throw new BadRequestException("接口不存在") ;
        }
        BeanUtils.copyProperties(permissionDto, permission, "id");
        permission.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        permission.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        permission.setLastUpdateTime(new Date());
        this.permissionDao.save(permission) ;
        PermissionVo permissionVo = new PermissionVo();
        BeanUtils.copyProperties(permission, permissionVo);
        return permissionVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cancelPermission(Integer permissionId) {
        TbPermission permission = this.permissionDao.getTbPermissionById(permissionId);
        log.debug("entity={}",permission);
        if (permission==null){
            throw new BadRequestException("接口不存在") ;
        }
        permission.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
        this.permissionDao.save(permission) ;
    }

    @Transactional
    @Override
    public PermissionVo addOrUpdatePermission(PermissionDto permissionDto) {
        if (StringUtils.isBlank(permissionDto.getApiUrl())){
            //throw new BadRequestException("url必填") ;
            return null;
        }
        if (permissionDto.getId()!=null){
            TbPermission permission = this.permissionDao.getTbPermissionById(permissionDto.getId());
            if (permission==null){
                throw new BadRequestException("接口ID错误") ;
            }
            if (!permission.getApiUrl().equals(permissionDto.getApiUrl())){
                throw new BadRequestException("url错误") ;
            }
            return this.updateIfModifyColumn(permissionDto,permission) ;
        } else {
            TbPermission permission = permissionDao.findByApiUrl(permissionDto.getApiUrl());
            if (permission!=null){
                return this.updateIfModifyColumn(permissionDto, permission) ;
            } else {
                return this.addPermission(permissionDto) ;
            }
        }
    }

    private PermissionVo updateIfModifyColumn(PermissionDto permissionDto, TbPermission permission){
        permission.setSystemId(permissionDto.getSystemId());
        if (StringUtils.isNotBlank(permissionDto.getApiName())){
            permission.setApiName(permissionDto.getApiName());
        }
        if (StringUtils.isNotBlank(permissionDto.getApiDesc())){
            permission.setApiDesc(permissionDto.getApiDesc());
        }
        permission.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
        this.permissionDao.save(permission) ;
        PermissionVo permissionVo = new PermissionVo();
        BeanUtils.copyProperties(permission, permissionVo);
        return permissionVo;
    }


    @Override
    public PermissionVo getPermissionInfo(Integer permissionId) {
        List<Integer> idList = new ArrayList<>();
        idList.add(permissionId);
        List<PermissionVo> permissionVoList = permissionDao.findByIdList(idList);
        if (permissionVoList==null||permissionVoList.isEmpty()){
            throw new BadRequestException("接口不存在") ;
        }
        return permissionVoList.get(0);
    }

    @Override
    public List<PermissionVo> getAllPermission(List<Integer> permissionIdList) {
        return permissionDao.findByIdList(permissionIdList);
    }

}
